﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Diagnostics;

namespace Splittergame
{
    class SmokeParticle
    {
        public int m_seed;
        private Vector2 SmokePosition;
        private Vector2 velocity;
        private Vector2 startVelocity;
        private Vector2 acceleration;
        private Vector2 origin;
        private Texture2D smokeTexture;
        float rotation;
        float killSpeed;
        float particleLifeTime = 3f;
        float size;
        float fade = 0.5f;


        public SmokeParticle(int seed, Texture2D texture, Vector2 startPosition)
        {
            m_seed = seed;
            smokeTexture = texture;
            acceleration = new Vector2(0, -0.1f);
            Random rand = new Random(m_seed);
            SmokePosition = startPosition;
            origin = new Vector2(6, 6);

            velocity = new Vector2((float)(rand.NextDouble() * 1.0 - 0.5), (float)(rand.NextDouble() * 1.0 - 0.5));
            startVelocity = velocity;

            float minSize = 2.5f;
            float maxSize = 5.0f;
            size = minSize + ((float)(rand.NextDouble()) * (maxSize - minSize));

            float minKillSpeed = 0.01f;
            float maxKillSpeed = 0.03f;           
            killSpeed = ((float)(rand.NextDouble()) * (maxKillSpeed - minKillSpeed));

        }

        internal void Update(GameTime elapsedTimeSeconds)
        {
            Random rand = new Random(m_seed);
            velocity = velocity + acceleration * elapsedTimeSeconds.ElapsedGameTime.Milliseconds / 100;
            SmokePosition = SmokePosition + velocity * elapsedTimeSeconds.ElapsedGameTime.Milliseconds / 500;

            float elapsed = elapsedTimeSeconds.ElapsedGameTime.Milliseconds;

            rotation = rotation + (elapsed * particleLifeTime)/1000;
            particleLifeTime = particleLifeTime - killSpeed;

            fade -= (float)elapsedTimeSeconds.ElapsedGameTime.TotalSeconds/5;

            if (particleLifeTime <= 0)
            {
                fade = 0.5f;
                //SmokePosition.X = 5;
                //SmokePosition.Y = 5;

                particleLifeTime = 3;
                velocity = new Vector2((float)(rand.NextDouble() * 1.0 - 0.5), (float)(rand.NextDouble() * 1.0 - 0.5f));
            }
        }

        internal void Draw(SpriteBatch spriteBatch, Camera camera)
        {
            Rectangle ballViewRect = camera.getVisualRect(SmokePosition.X, SmokePosition.Y, 0.2f);
            Vector2 positionVector = new Vector2(ballViewRect.X, ballViewRect.Y);

            Color color = new Color(fade, fade, fade, fade);
            spriteBatch.Draw(smokeTexture, positionVector, null, color, rotation, origin, size, SpriteEffects.None, 0);
        }
    }
}
